﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content.Pipeline;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
using System.IO;
using Microsoft.Xna.Framework;
using System.Xml.Serialization;
using System.Diagnostics;

namespace XNADota.ContentPipeline.Terrain
{
    [ContentSerializerRuntimeType("XNADota.Terrain.Landscape, XNADota")]
    public class LandscapeContent
    {
        private LandscapeConfigContent _LandscapeConfig;

        private WarCraftEnvironmentContent _WarCraftEnvironment;

        private WarCraftMapDooContent _WarCraftMapDoo;

        private WarCraftUnitsDooContent _WarCraftUnitsDoo;

        private WarCraftCustomUnitContent _WarCraftCustomUnit;

        private TexturePaletteContent[] _TexturePallettes;

        public LandscapeConfigContent LangscapeConfig
        {
            set { _LandscapeConfig = value; }
            get { return _LandscapeConfig; }
        }

        public TexturePaletteContent[] TexturePallettes
        {
            set { _TexturePallettes = value; }
            get { return _TexturePallettes; }
        }

        public WarCraftEnvironmentContent WarCraftEnvironment
        {
            set { _WarCraftEnvironment = value; }
            get { return _WarCraftEnvironment; }
        }

        public WarCraftMapDooContent WarCraftMapDoo
        {
            set { _WarCraftMapDoo = value; }
            get { return _WarCraftMapDoo; }
        }

        public WarCraftUnitsDooContent WarCraftUnitsDoo
        {
            set { _WarCraftUnitsDoo = value; }
            get { return _WarCraftUnitsDoo; }
        }

        public WarCraftCustomUnitContent WarCraftCustomUnit
        {
            set { _WarCraftCustomUnit = value; }
            get { return _WarCraftCustomUnit; }
        }

        public LandscapeContent(LandscapeConfigContent LandscapeConfig, ContentProcessorContext context)
        {
            System.Diagnostics.Debugger.Launch();

            _LandscapeConfig = LandscapeConfig;

            string directory = Path.GetDirectoryName(_LandscapeConfig.ConfigFilePath);

            // War Craft Environment, maps surfaces and cliffs
            string pathName = Path.Combine(directory, _LandscapeConfig.WarCraftEnvironmentFileName);
            using (FileStream fileStream = new FileStream(pathName, FileMode.Open))
            {
                WCLoader Loader = new WCLoader(pathName, fileStream);
                this._WarCraftEnvironment = new WarCraftEnvironmentContent(Loader);
            }

            pathName = Path.Combine(directory, _LandscapeConfig.WarCraftMapDooFileName);
            using (FileStream fileStream = new FileStream(pathName, FileMode.Open))
            {
                WCLoader Loader = new WCLoader(pathName, fileStream);
                this._WarCraftMapDoo = new WarCraftMapDooContent(Loader);
            }

            pathName = Path.Combine(directory, _LandscapeConfig.WarCraftUnitsDooFileName);
            using (FileStream fileStream = new FileStream(pathName, FileMode.Open))
            {
                WCLoader Loader = new WCLoader(pathName, fileStream);
                this._WarCraftUnitsDoo = new WarCraftUnitsDooContent(Loader);
            }


            pathName = Path.Combine(directory, _LandscapeConfig.WarCraftCustomUnitsFileName);
            using (FileStream fileStream = new FileStream(pathName, FileMode.Open))
            {
                WCLoader Loader = new WCLoader(pathName, fileStream);
                this._WarCraftCustomUnit = new WarCraftCustomUnitContent(Loader);
            }

            _TexturePallettes = new TexturePaletteContent[LandscapeConfig.TexturePaletteFileNames.Length];
            for (int i = 0; i < _TexturePallettes.Length; i++)
                _TexturePallettes[i] = new TexturePaletteContent(
                    Path.Combine(directory, LandscapeConfig.TexturePaletteFileNames[i]), context);
        }
    }
}
